﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using EasyToHelp.Business;
using EasyToHelp.Entity;

namespace EasyToHelp.WEBSystem.Controllers
{
    public class GridManageController : Controller
    {
        public IActionResult Index(string Table)
        {
            if (!string.IsNullOrWhiteSpace(Table))
            {
                ViewBag.Table = Table;
                var list = GridBusiness.GetGridManageFieldList(Table);
                return View(list);
            }
            return View();
        }
        public IActionResult GridHandle(string TableName)
        {
            return View();
        }

        public IActionResult GridConf(string Table)
        {
            if (string.IsNullOrWhiteSpace(Table))
            {
                return Content("缺少参数或者参数不正确！");
            }
            ViewBag.CodeName = Table;
            var list = GridBusiness.GetGridManageFieldList(Table);
            if (list.Count > 0)
            {
                ViewBag.BusinessName = list.FirstOrDefault().BusinessName;
                return View(list);
            }
            else
            {
                ViewBag.BusinessName = Table;
            }
            return View();
        }

        public IActionResult GridView(string Table)
        {
            if (!string.IsNullOrWhiteSpace(Table))
            {
                ViewBag.Table = Table;
                var list = GridBusiness.GetGridManageFieldList(Table);                
                List<string> fieldList = new List<string>();
                StringBuilder sb = new StringBuilder();
                if (list.Count > 0)
                {
                    fieldList.Add("                             {type:'checkbox', fixed: true}\r\n");
                    list.ForEach(x =>
                    {
                        if (x.IsSearchPkName)
                        {
                            if (x.IsFixed)
                            {
                                fieldList.Add("                 { field: '" + x.EnglishName + "', title: '" + x.ChineseName + "', fixed: true }\r\n");
                            }
                            else if (x.IsSort)
                            {
                                fieldList.Add("                 { field: '" + x.EnglishName + "', title: '" + x.ChineseName + "', sort: true }\r\n");
                            }
                            else if (x.IsFixed && x.IsSort)
                            {
                                fieldList.Add("                 { field: '" + x.EnglishName + "', title: '" + x.ChineseName + "', fixed: true,sort: true }\r\n");
                            }
                            else
                            {
                                fieldList.Add("                 { field: '" + x.EnglishName + "', title: '" + x.ChineseName + "'}\r\n");
                            }
                        }
                        if (x.IsWhere)
                        {
                            switch (x.FieldType)
                            {
                                case "text":
                                  sb.Append(WhereList.TextString(x.ChineseName,x.EnglishName));
                                    break;
                                case "radio":
                                    sb.Append(WhereList.RadioString());
                                    break;
                                case "select":
                                    sb.Append(WhereList.SelectString());
                                    break;
                                case "bit":
                                    sb.Append(WhereList.BoolString());
                                    break;
                                case "date":
                                    sb.Append(WhereList.DateString());
                                    break;
                            }
                        }
                    });
                    fieldList.Add("                 {fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}");
                    ViewBag.FieldList = string.Join(',', fieldList.ToArray());
                    ViewBag.WhereList = sb.ToString();
                }
            }
            return View();
        }

        
        public IActionResult GridData(string Table, string WhereJson = "")
        {
            Datajson model = new Datajson();
            model.code = "0";
            model.count = "100";
            model.data = GridBusiness.GetGridData(Table, WhereJson);
            return Json(model);
        }

        public IActionResult synTableManageField(string TableName)
        {
            return Json(GridBusiness.synTableManageField(TableName));
        }
    }
}